package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@Service
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;
    @Autowired
    private VodUploadClient vodUploadClient;

    @Override
    public TrackMediaInfoVo getTrackMediaInfo(String mediaFileId) {
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性
            // 以下代码示例仅供参考，建议采用更安全的方式来使用密钥
            // 请参见：https://cloud.tencent.com/document/product/1278/85305
            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
            Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());

            // 实例化要请求产品的client对象
            VodClient client = new VodClient(cred, vodConstantProperties.getRegion());
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
            String[] fileIds1 = {mediaFileId};
            req.setFileIds(fileIds1);

            // 返回的resp是一个DescribeMediaInfosResponse的实例，与请求对象对应
            DescribeMediaInfosResponse resp = client.DescribeMediaInfos(req);
            //判断req获取media_duration media_url media_size media_type
            if (resp!=null){
                MediaInfo[] mediaInfoSet = resp.getMediaInfoSet();
                MediaBasicInfo basicInfo = mediaInfoSet[0].getBasicInfo();
                //获取媒体类型 mp3
                String type = basicInfo.getType();

                MediaMetaData metaData = mediaInfoSet[0].getMetaData();
                //获取时长
                Float duration = metaData.getDuration();
                //获取大小
                Long size = metaData.getSize();
                //创建封装对象
                TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();

                trackMediaInfoVo.setType(type);
                trackMediaInfoVo.setDuration(duration);
                trackMediaInfoVo.setSize(size);

                return trackMediaInfoVo;
            }
        } catch (TencentCloudSDKException e) {
            log.error("[专辑服务]获取点播平台文件：{}，详情异常：{}", mediaFileId, e);
            throw new GuiguException(400, "音频文件详情获取异常！");
        }
        return null;
    }
    /**
     * 上传声音
     * @param file
     * @return
     */

    @Override
    public Map<String, String> uploadTrack(MultipartFile file) {
        try {
            String tempPath = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(), file);
            VodUploadClient client = new VodUploadClient(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
            VodUploadRequest request = new VodUploadRequest();
            request.setMediaFilePath(tempPath);
            VodUploadResponse uploadResponse = client.upload(vodConstantProperties.getRegion(), request);
            if (uploadResponse != null) {
                Map<String, String> mapResult = new HashMap<>();
                mapResult.put("mediaFileId", uploadResponse.getFileId());
                mapResult.put("mediaUrl", uploadResponse.getMediaUrl());
                return mapResult;
            }
        } catch (Exception e) {
            // 业务方进行异常处理
            log.error("文件上传异常" + e);
            throw new RuntimeException(e);
        }
        return null;
    }


}
